import base64
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad


class EncryptData:  # 构造函数,初始函数
    def __init__(self, key):
        self.key = key

    def encrypt(self, data):
        data = data.encode('utf-8')
        cipher = AES.new(self.key, AES.MODE_ECB)
        ct_bytes = cipher.encrypt(pad(data, AES.block_size))
        return base64.b64encode(ct_bytes).decode('utf-8')


# 调用函数
if __name__ == '__main__':
    key = b"1234567812345678"
    data = '123456'
    ed = EncryptData(key)  # 实例化
    result = ed.encrypt(data)
    print(result)
